package com.ng.desktop.expansionsa.view.articulo;

import com.jidesoft.swing.AutoCompletionComboBox;
import com.ng.desktop.expansionsa.model.Articulo;
import com.ng.desktop.expansionsa.services.CustomNGListingService;
import com.ng.desktop.expansionsa.services.NGFactoryBean;
import com.ng.framework.persistence.NGSearchParameter.Type;
import com.ng.framework.persistence.NGSearchParameter.UnaryOperation;
import com.ng.framework.view.desktop.filters.NGSimpleFilter;

public class FilterCategoria extends NGSimpleFilter<AutoCompletionComboBox> {
	
	private static final long serialVersionUID = 7549401894962395565L;
	
	private CustomNGListingService customNGListingService;

	public FilterCategoria() {
		super("Categoría", Articulo.class, "categoria", Type.STRING, UnaryOperation.EQUALS);
		
		this.actualizarCategorias();
	}
	
	public void actualizarCategorias() {
		this.getComponent().removeAllItems();
		
		this.getComponent().addItem(null);
		for(String categoria : this.getCustomNGListingService().getCategorias()) {
			this.getComponent().addItem(categoria);	
		}
	}
	
	private CustomNGListingService getCustomNGListingService() {
		if(customNGListingService == null) {
			customNGListingService = (CustomNGListingService)NGFactoryBean.getInstance().getBean("customNGListingService");
		}
		
		return customNGListingService;
	}

	@Override
	protected AutoCompletionComboBox createComponent() {
		return new AutoCompletionComboBox();
	}

	@Override
	protected String getComponentValue(AutoCompletionComboBox component) {
		return (String)component.getSelectedItem();
	}

	@Override
	public void clear() {
		this.getComponent().setSelectedItem(null);
	}
	
}